package com.dhcc.sds.base.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.cache.formitemmatchrule.IFormItemMatchRuleCache;
import com.dhcc.sds.base.entity.SdsQcfitemMatchRuleEntity;
import com.dhcc.sds.base.service.ISdsQcfitemMatchRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;


/**
 * 项目校验规则表
 * 
 * @ClassName: SdsQcfitemMatchRuleController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-04-15 11:14:54
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsqcfitemmatchrule")
public class SdsQcfitemMatchRuleController extends BaseController {

	private String PREFIX = "/backend/sds/sdsqcfitemmatchrule/";

	@Autowired
	private ISdsQcfitemMatchRuleService sdsQcfitemMatchRuleService;
	

    /**
     * 跳转到项目校验规则表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsqcfitemmatchrule.html";
    }
	
    /**
     * 跳转到新增项目校验规则表
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsqcfitemmatchrule_add.html";
    }
	
	/**
     * 跳转到修改项目校验规则表
     */
    @RequestMapping("/to_update/{sdsQcfitemMatchRuleId}")
    public String toUpdate(@PathVariable Long sdsQcfitemMatchRuleId, Model model) {
        SdsQcfitemMatchRuleEntity sdsQcfitemMatchRule = this.sdsQcfitemMatchRuleService.selectById(sdsQcfitemMatchRuleId);
        model.addAttribute("sdsQcfitemMatchRule",sdsQcfitemMatchRule);
        return PREFIX + "sdsqcfitemmatchrule_edit.html";
    }

    /**
     * 获取项目校验规则表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsQcfitemMatchRuleEntity> page = new PageFactory<SdsQcfitemMatchRuleEntity>().dataGridPage();
        List<SdsQcfitemMatchRuleEntity> result = this.sdsQcfitemMatchRuleService.page(page,params);
        page.setRecords((List<SdsQcfitemMatchRuleEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增项目校验规则表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @CacheClear(IFormItemMatchRuleCache.CACHE_NAME)
    public Object add(SdsQcfitemMatchRuleEntity sdsQcfitemMatchRule) {
        if (CommonUtil.isOneEmpty(sdsQcfitemMatchRule)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsQcfitemMatchRule.insert();
        return R.ok();
    }

    /**
     * 删除项目校验规则表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @CacheClear(IFormItemMatchRuleCache.CACHE_NAME)
    public Object delete(@RequestParam Long sdsQcfitemMatchRuleId) {
	
        this.sdsQcfitemMatchRuleService.deleteById(sdsQcfitemMatchRuleId);

        return R.ok();
    }

    /**
     * 修改项目校验规则表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @CacheClear(IFormItemMatchRuleCache.CACHE_NAME)
    public Object update(SdsQcfitemMatchRuleEntity sdsQcfitemMatchRule) {
        if (CommonUtil.isOneEmpty(sdsQcfitemMatchRule)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsQcfitemMatchRuleEntity old = this.sdsQcfitemMatchRuleService.selectById(sdsQcfitemMatchRule.getId());

        old.setFormItemId(sdsQcfitemMatchRule.getFormItemId());
        old.setControlCode(sdsQcfitemMatchRule.getControlCode());
        old.setControlDesc(sdsQcfitemMatchRule.getControlDesc());
        try {
            old.setControlExp(java.net.URLDecoder.decode(sdsQcfitemMatchRule.getControlExp(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        old.setControlLevel(sdsQcfitemMatchRule.getControlLevel());
        old.setIndNo(sdsQcfitemMatchRule.getIndNo());
        old.setTipText(sdsQcfitemMatchRule.getTipText());
        old.setUpdateUser(sdsQcfitemMatchRule.getUpdateUser());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
